﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using ThoiTrang.Business;
using ThoiTrang.Entities;
using System.Text;

public partial class adm_surveyoption : System.Web.UI.Page
{
    #region "Page functions"

    public string ckid = "";
    public int totalRes = 0;

    private void _BindGreeting()
    {
        a_User au=a_UserService.Geta_User(Request.Params["ckid"]);
        if (au != null)
        {
            this.lblGretting.Text = "Hi " + au.Firstname + " " + au.Lastname;
        }
        if(Request.Params["qid"]!=null)
        {
            a_Survey asv=a_SurveyService.Geta_Survey(int.Parse(Request.Params["qid"].ToString()));
            this.lblQuestion.Text = asv.Question;
        }
    }

    private void _BindRepeater()
    {
        if (Request.Params["qid"] != null)
        {
            int qid = int.Parse(Request.Params["qid"].ToString());
            if (Request.Params["fn"] == null | (Request.Params["fn"] == "#" | Request.Params["fn"] == ""))
            {
                this.rptSurvey.DataSource = a_Surveys_OptionService.Geta_Surveys_OptionList_qid(qid, a_Surveys_OptionColumns.RespCount, ThoiTrang.Data.OrderDirection.DESC);
                this.rptSurvey.DataBind();
            }
            else
            {
                this.rptSurvey.DataSource = a_Surveys_OptionService.Geta_Surveys_OptionList_alphabet(qid, Request.Params["fn"].ToString(), a_Surveys_OptionColumns.RespCount, ThoiTrang.Data.OrderDirection.DESC);
                this.rptSurvey.DataBind();
            }
        }
    }

    private void _BindAlphabetSearcher()
    {
        if (Request.Params["qid"] != null)
        {
            DataTable dt = ThoiTrang.Data.General.ExecSQLStatement("SELECT DISTINCT SUBSTRING(RespOption, 1, 1) AS Name FROM a_Surveys_Options WHERE QID=" + Request.Params["qid"].ToString()+ " ORDER BY Name");
            this.rptAlphabetSearch.DataSource = dt;
            this.rptAlphabetSearch.DataBind();
        }
    }

    private void _ReadParam()
    {
        try
        {
            if (Request.Params["catid"] != null && Request.Params["act"] == null)
            {
                a_Surveys_Option acy = a_Surveys_OptionService.Geta_Surveys_Option(int.Parse(Request.Params["catid"]));
                if (acy != null)
                {
                    this.txtbodyVN.Text = acy.RespOption;
                    this.btnUpdate.Visible = true;
                    this._BindRepeater();
                }
            }
            else if (Request.Params["catid"] != null && Request.Params["act"] == "del")
            {
                //Kiem tra xem co category nao khong?
                a_Surveys_OptionService.Deletea_Surveys_Option(int.Parse(Request.Params["catid"]));
                CommonFunc.ShowAlertMessage(this.lblAlert, "Delete sucessfully");
                this._BindRepeater();
            }
            else if (Request.Params["fn"] != null)
            {
                this._BindRepeater();
            }
        }
        catch (Exception ex)
        {
            //throw new exception here
        }
    }

    public string GetActiveClass(string fn)
    {
        if (Request.Params["fn"] != null && Request.Params["fn"] == fn)
        {
            return "active";
        }
        else
        {
            return "";
        }
    }

    public string GetActiveClassAll()
    {
        if (Request.Params["fn"] == null)
        {
            return "active";
        }
        else
        {
            return "";
        }
    }

    public int Counttotalresponse()
    {
        int iReturn = 0;
        try
        {
            if (Request.Params["qid"] != null)
            {
                iReturn = int.Parse(ThoiTrang.Data.General.ExecSQLStatement("SELECT SUM([RespCount]) FROM [a_Surveys_Options] WHERE [QID] = " + Request.Params["qid"].ToString()).Rows[0][0].ToString());
            }
        }
        catch (Exception ex)
        {
            iReturn = 0;
        }
        return iReturn;
    }

    public string Calculatepercent(int count)
    {

        decimal percent = 0;
        if (totalRes != 0)
        {
            percent = Decimal.Divide(count, totalRes) * 100;
        }
        return percent.ToString("0.000") + "%";
    }

    public string CreateEditDeleteCommentLink(int catid,int quesId)
    {
        string hLink = "";
        if (Request.Params["ckid"] != null)
        {
            hLink = "<a href='adm_surveyoption.aspx?ckid=" + Request.Params["ckid"].ToString() + "&catid=" + catid.ToString() + "&fn={0}&qid=" + quesId.ToString() + "'><img src='../images/ico_edit.gif' alt='edit' width='12' height='12' /></a><a onclick=" + "\"" + "return confirm('Are you sure want to delete?')" + "\"" + "' href='adm_surveyoption.aspx?ckid=" + Request.Params["ckid"].ToString() + "&catid=" + catid.ToString() + "&act=del" + "&fn={0}&qid=" + quesId.ToString() + "'><img src='../images/ico_delete.gif' alt='del' width='16' height='13' /></a>";
        }
        else
        {
            hLink = "<a href='adm_surveyoption.aspx?catid=" + catid.ToString() + "&fn={0}&qid="+quesId.ToString()+"'><img src='../images/ico_edit.gif' alt='edit' width='12' height='12' /></a><a href='adm_surveyoption.aspx?catid=" + catid.ToString() + "&act=del" + "&fn={0}&qid="+quesId.ToString()+"'><img src='../images/ico_delete.gif' alt='del' width='16' height='13' /></a>";
        }
        if (Request.Params["fn"] != null && (Request.Params["fn"] != "#" | Request.Params["fn"] != ""))
        {
            hLink = hLink.Replace("{0}", Request.Params["fn"].ToString());
        }
        else
        {
            hLink = hLink.Replace("{0}", "");
        }
        return hLink;
    }

    public string _CreateLinkforAll()
    {
        if (Request.Params["ckid"] != null && Request.Params["qid"]!=null)
        {
            return "adm_surveyoption.aspx?ckid=" + Request.Params["ckid"].ToString()+"&qid="+Request.Params["qid"].ToString();
        }
        else
        {
            return "";
        }
    }

    private bool CheckisQuestionExistence(string quescontent)
    {
        if(Request.Params["qid"]!=null)
        {
            foreach(a_Surveys_Option aso in a_Surveys_OptionService.Geta_Surveys_OptionList_qid(int.Parse(Request.Params["qid"].ToString()),a_Surveys_OptionColumns.RespOption,ThoiTrang.Data.OrderDirection.ASC))
            {
                if(aso.RespOption==quescontent)
                {
                    return true;
                }
            }
        }
        return false;
    }
    #endregion

    #region "Event"
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Params["ckid"] != null)
        {
            ckid = Request.Params["ckid"];
        }
        if (!this.IsPostBack)
        {
            totalRes = this.Counttotalresponse();
            this._BindRepeater();
            this._ReadParam();
            this._BindGreeting();
            this._BindAlphabetSearcher();
        }
        
    }

    protected void btnCommit_Click(object sender, EventArgs e)
    {
        try
        {
            if (!this.CheckisQuestionExistence(this.txtbodyVN.Text))
            {
                a_Surveys_Option acy = new a_Surveys_Option();
                acy.RespOption = this.txtbodyVN.Text;
                acy.QID = int.Parse(Request.Params["qid"].ToString());
                a_Surveys_OptionService.Createa_Surveys_Option(acy);
                CommonFunc.ShowAlertMessage(this.lblAlert, "Add new survey sucessfully");
                this._BindRepeater();
                //this.rptSurvey.Items[0].BackColor = System.Drawing.Color.FromName("#CCFF66");
            }
            else
            {
                CommonFunc.ShowAlertMessage(this.lblAlert, "The response option already existed");
            }
        }
        catch (Exception ex)
        {
            //throw new error
        }
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        try
        {
            if (!this.CheckisQuestionExistence(this.txtbodyVN.Text))
            {
                a_Surveys_Option acy = a_Surveys_OptionService.Geta_Surveys_Option(int.Parse(Request.Params["catid"]));
                if (acy != null)
                {
                    acy.RespOption = this.txtbodyVN.Text;
                    this.btnUpdate.Visible = false;
                    a_Surveys_OptionService.Updatea_Surveys_Option(acy);
                    CommonFunc.ShowAlertMessage(this.lblAlert, "Update response option sucessfully");
                    this._BindRepeater();
                }
            }
            else
            {
                CommonFunc.ShowAlertMessage(this.lblAlert, "The response option already existed");
            }
        }
        catch (Exception ex)
        {
            //throw new exception here
        }
    }

    protected void drpStatus_SelectedIndexChanged(object sender, EventArgs e)
    {
        this._BindRepeater();
    }

    protected void rptSurvey_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {

    }

    #endregion 
   
}
